package learn.demo;

import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.DuplexChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

//管道初始化器
// 当前累的实例在pipeline初始化完毕后就会被GC
public class ChannelInitializer<S extends DuplexChannel> extends io.netty.channel.ChannelInitializer<SocketChannel> {

    //当Channel初始创建完成后就会触发该方法的执行，用于初始化Channel
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //从Channel中获取pipeline
        ChannelPipeline pipeline = socketChannel.pipeline();
        //将HttpServercodec处理器放入到pipeline的最后
        //HttpServerCodec是HttpRequestDecoder和HttpResponseEncoder的复合体
        //HttpRequestDecoder：http请求解码器，将Channel中的ByteBuf数据解码为HttpRequest对象
        //HttpResponseEncoder：http响应编码器，将HttpReponse对象编码为将要在Channel中发送的ByteBuf数据
        pipeline.addLast(new HttpServerCodec());
        //将自定义的处理器放入到pipeline的最后
        pipeline.addLast(new ServerHandler());
    }
}
